<?php
namespace app\question\controller;
use think\Controller;

use app\question\controller\Common;

use app\model\QuestionModel;



use app\company\service\DataService;
// use library\Controller;
use think\Db;

class Question1 extends Controller{
	
	public function index(){
		$page = trim($this->request->param('page'));
		$pagesize = trim($this->request->param('pagesize'));
		$keyword = trim($this->request->param('keyword'));

		$page = empty($page) ? 1 : $page;
		$pagesize = empty($pagesize) ? 10 : $pagesize;
		
		$where = $keyword ? ":name like %".$keyword."%" : '1=1';
		$list = Db::name('QuestionList')->order('id asc')->select();

		$this->assign('nav',null);
		$this->assign('pageRes',null);
		$this->assign('list',$list);
				// $this->assign('menu',null);

		return $this->fetch();		
	}
	
	public function add(){
		if($_POST){

			$question = trim($this->request->param('question'));
			$option1 = trim($this->request->param('option1'));
			$option2 = trim($this->request->param('option2'));
			$option3 = trim($this->request->param('option3'));
			$answer = trim($this->request->param('answer'));


			if( empty($question) || empty($option1) || empty($option2) || empty($option3) || empty($answer) ){
				return ['message'=>'参数不全','code'=>400];
			}

			if( $answer < 1 || $answer > 4 ){
				return ['message'=>'答案填写出错，请选择正确答案','code'=>400];
			}
			
			$questionModel = new QuestionModel;
			$questionModel->companyid = $this->user->companyid;
			$questionModel->question = $question;
			$questionModel->options = $option1.",".$option2.",".$option3;
			$questionModel->answer = $answer;
			$questionModel->save();

			if( $questionModel->id > 0 ){
				return ['message'=>'新增题目成功','code'=>'200'];
	
			}else{
				return ['message'=>'新增题目出错，请联系客服！','code'=>'400'];
			}
	
		}else{
			return $this->fetch(ROOT_PATH .'application/admin/view/Question/add.html');
		}
	}
	
	public function edit(){
		$id = $this->request->param('id');
		$question = QuestionModel::where('id',$id)->find();
		
		$arr = explode(',',$question->options);
		$option1 = $arr[0];
		$option2 = $arr[1];
		$option3 = $arr[2];

		$this->assign('option1',$option1);
		$this->assign('option2',$option2);
		$this->assign('option3',$option3);
		$this->assign('question',$question);

		return $this->fetch(ROOT_PATH .'application/admin/view/Question/edit.html');		
	}
	
	public function save(){
		$id = trim($this->request->param('id'));
		$question = trim($this->request->param('question'));
		$option1 = trim($this->request->param('option1'));
		$option2 = trim($this->request->param('option2'));
		$option3 = trim($this->request->param('option3'));
		$answer = trim($this->request->param('answer'));


		if( empty($question) || empty($option1) || empty($option2) || empty($option3) || empty($answer) ){
			return ['message'=>'参数不全','code'=>400];
		}

		if( $answer < 1 || $answer > 4 ){
			return ['message'=>'答案填写出错，请选择正确答案','code'=>400];
		}
			

		$questionModel = QuestionModel::where('id',$id)->find();
		$questionModel->question = $question;
		$questionModel->options = $option1.",".$option2.",".$option3;
		$questionModel->answer = $answer;
		$questionModel->save();

		return ['message'=>'保存成功','code'=>200];

	}
	
	public function delete(){
		$id = trim($this->request->param('id'));

        if(empty($id)){
            return ['message'=>'ID不能为空','code'=>400];
        }

        $question = QuestionModel::where('id','eq',$id)->find();
        if(!$question){
            return ['message'=>'问题不存在','code'=>'400'];
        }

        $question->delete();

        return ['message'=>'删除问题成功','code'=>'200'];
	}
	
	/**
	 * 数据排序
	 */
	// public function listorder(){
	// 	$listoreder=$_POST['listorder'];
	// 	$data =array('jump_url'=> $_SERVER['HTTP_REFERER']);
	// 	$errors=array();
	// 	if($listoreder){
	// 		try{
	// 			foreach($listorder as $emnuId=>$v){
	// 				$id=D("Menu")->updateMenuListorderById($menuId,$v);
	// 				if($id===false){
	// 					$errors[]=$menuId;
	// 				}
	// 			}
	// 		}catch(Exception $ex){
	// 			return jsonResult(0, $ex->getMessage(), $data);
	// 		}
	// 		if($errors){
	// 			return jsonResult(0,"排序失败-".implode(',', $errors), $data);
	// 		}
	// 		return jsonResult(1, '排序成功', $data);
	// 	}
	// 	return jsonResult(0,'数据排序失败', $data);
	// }
}
?>